package com.golddaymicromall.activity.salemanager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.activity.R;
import com.golddaymicromall.adapter.SaleManagerAdapter;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.bean.OrderInfo;
import com.golddaymicromall.bean.StoreDetailInfo;
import com.golddaymicromall.util.RopUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.util.ServerConfig;
import com.google.common.collect.Maps;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okrx.RxAdapter;

import java.util.List;
import java.util.Map;

import rx.android.schedulers.AndroidSchedulers;

public class SaleManagerActivity extends BaseActivity implements View.OnClickListener {

    private LinearLayout mSaleManagerBack;
    private ListView mSaleListView;

    private List<OrderInfo> listOrderInfo;
    private SaleManagerAdapter saleManagerAdapter;
    private LinearLayout mGoPurchase;
    private String logid;
    int flag = 1;
    ProgressBar progress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sale_manager);
        initView();
        SPUtils.put(SaleManagerActivity.this, "saleflag", "0");
    }

    @Override
    protected void onResume() {
        super.onResume();
        attemptGetOrderInfo();
        attemptGetCustomerType();
    }

    public void initView() {
        progress = (ProgressBar) findViewById(R.id.progress);
        mSaleManagerBack = (LinearLayout) findViewById(R.id.sale_back);
        mSaleListView = (ListView) findViewById(R.id.sale_listview);
        mGoPurchase = (LinearLayout) findViewById(R.id.goPurchase);
        mSaleManagerBack.setOnClickListener(this);
        mGoPurchase.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sale_back:
                finish();
                break;

            case R.id.goPurchase:
                Intent intent_purchase = new Intent(this, SalePurchaseActivity.class);
                startActivity(intent_purchase);
                break;
        }
    }

    private void attemptGetCustomerType() {
        //获取本地数据
        logid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.customer.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("id", logid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    List<StoreDetailInfo> storeDetail;
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                        } else if ("null".equals(result)) {
                        } else {
                            storeDetail = JSON.parseArray(result, StoreDetailInfo.class);
                            SPUtils.put(getApplicationContext(), "shop_Grade", storeDetail.get(0).getShop_Grade());
                            SPUtils.put(getApplicationContext(), "turndate", storeDetail.get(0).getTurndate() == null ? "" : storeDetail.get(0).getTurndate());
                        }
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                });
    }

    private void attemptGetOrderInfo() {
        String logid = SPUtils.get(getApplicationContext(), "Id", "").toString();
        Map<String, String> form = Maps.newHashMap();
        form.put("method", "rop.order.get");
        form.put("appKey", "00001");
        form.put("v", "1.0");
        form.put("format", "json");
        form.put("customerid", logid);
        String sing = RopUtils.signString(form, "qwertyuiop");
        form.put("sign", sing);
        OkGo.post(new ServerConfig().SERVER)
                .params(form)
                .getCall(StringConvert.create(), RxAdapter.<String>create())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(result -> {
                    if (!"".equals(result)) {
                        if (result.contains("errorToken") || "0".equals(result)) {
                            Toast.makeText(getApplicationContext(), "访问服务器失败,请稍后重试", Toast.LENGTH_SHORT).show();
                        } else if ("null".equals(result)) {
                            SPUtils.put(getApplicationContext(), "firstFlag", "0");
                            listOrderInfo.clear();
                        } else {
                            listOrderInfo = JSON.parseArray(result, OrderInfo.class);
                            SPUtils.put(getApplicationContext(), "firstFlag", listOrderInfo.size() + "");
                            saleManagerAdapter = new SaleManagerAdapter(listOrderInfo, SaleManagerActivity.this);
                            mSaleListView.setAdapter(saleManagerAdapter);
                            saleManagerAdapter.notifyDataSetChanged();
                        }
                    } else {
                        Toast.makeText(getApplicationContext(), "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
                    }
                }, throwable -> {
                    throwable.printStackTrace();
                });
    }
}
